﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;


public class TweenMake : MonoBehaviour
{
    public enum TweenType
    {
        PosX,
        PosY,
        PosXY,
        ScaleX,
        ScaleY,
        ScaleXY,
        AnglesX,
        AnglesY,
        AnglesZ,
        Width,
        Height,
        WidthAndHeight,
    }
    public TweenType tweenType = TweenType.PosX;
    public Vector3 value = Vector3.zero;
    public Ease ease = Ease.Linear;
    public float delay = 1f;
    public bool speed = false;
    public int loop = 1;
    public LoopType loopType = LoopType.Restart;

    private Tweener tw = null;
    Vector3 targetVal = Vector3.one;
    RectTransform rtf = null;

    public void Play()
    {
        if (rtf == null)
            rtf = transform.GetComponent<RectTransform>();
        if (tw != null)
            tw.Kill();

        tw = DOTween.To((t) =>
        {
            switch (tweenType)
            {
                case TweenType.PosX:
                    targetVal = rtf.localPosition;
                    targetVal.x = t;
                    rtf.localPosition = targetVal;
                    break;
                case TweenType.PosY:
                    targetVal = rtf.localPosition;
                    targetVal.y = t;
                    rtf.localPosition = targetVal;
                    break;
                case TweenType.PosXY:
                    targetVal = rtf.localPosition;
                    targetVal.x = t;
                    targetVal.y = t;
                    rtf.localPosition = targetVal;
                    break;
                case TweenType.ScaleX:
                    targetVal = rtf.localScale;
                    targetVal.x = t;
                    rtf.localScale = targetVal;
                    break;
                case TweenType.ScaleY:
                    targetVal = rtf.localScale;
                    targetVal.y = t;
                    rtf.localScale = targetVal;
                    break;
                case TweenType.ScaleXY:
                    targetVal = rtf.localScale;
                    targetVal.x = t;
                    targetVal.y = t;
                    rtf.localScale = targetVal;
                    break;
                case TweenType.AnglesX:
                    targetVal = rtf.localEulerAngles;
                    targetVal.x = t;
                    rtf.localEulerAngles = targetVal;
                    break;
                case TweenType.AnglesY:
                    targetVal = rtf.localEulerAngles;
                    targetVal.y = t;
                    rtf.localEulerAngles = targetVal;
                    break;
                case TweenType.AnglesZ:
                    targetVal = rtf.localEulerAngles;
                    targetVal.z = t;
                    rtf.localEulerAngles = targetVal;
                    break;
                case TweenType.Width:
                    targetVal = rtf.sizeDelta;
                    targetVal.x = t;
                    rtf.sizeDelta = targetVal;
                    break;
                case TweenType.Height:
                    targetVal = rtf.sizeDelta;
                    targetVal.y = t;
                    rtf.sizeDelta = targetVal;
                    break;
                case TweenType.WidthAndHeight:
                    targetVal = rtf.sizeDelta;
                    targetVal.x = t;
                    targetVal.y = t;
                    rtf.sizeDelta = targetVal;
                    break;
            }
        }, value.x, value.y, value.z);
        if (delay > 0)
            tw.SetDelay(delay);
        tw.SetEase(ease);
        if (loop > 1 || loop < 0)
            tw.SetLoops(loop, loopType);
        if (speed) tw.SetSpeedBased();
        tw.Restart();
    }

    public void AllPlay()
    {
        TweenMake[] tweenMakes = transform.GetComponentsInChildren<TweenMake>();
        foreach (var item in tweenMakes)
        {
            item.Play();
        }
    }

    public void PrintLuaCode()
    {
        string setDelay = delay>0? "tween:SetDelay("+ delay + ");\n" : "";
        string setSpeedBased = speed ? "tween:SetSpeedBased();\n" : "";
        string setLoops = loop > 1 || loop < 0 ? "tween:SetLoops("+ loop + ","+ "LoopType." + loopType.ToString()+ ");\n" : "";

        string luaCode = "local tween= DOTween.To(function(t)\n" +
            "       " + tweenType.ToString()
        +"\nend," + value.x + "," + value.y + "," + value.z + ");\n" + setDelay + setSpeedBased + setLoops + "tween.Restart();";
        Debug.Log(luaCode);
    }
}
